﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KerriganInterface.IServiceClient;
using KerriganDataContract;
using KerriganService.DAC;
using System.ServiceModel.Activation;
using System.IO;
using System.ServiceModel.Web;
using KerriganInterface;
using KerriganDataContract.OfficialTest;
using KerriganDataContract.Customer;
using KerriganDataContract.Code;
using KerriganDataContract.Session;
using KerriganDataContract.Facture;
using KerriganDataContract.Person;
using KerriganDataContract.Agency;

namespace KerriganService.ServiceClient
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AgencyService" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AgencyService : IAgencyService, IPolicyRetriever
    {


        public CustomerDC GetCustomer(int id)
        {
            try
            {
               return CustomerDAC.GetCustomer(id);

            }
            catch (Exception ex) {
                throw new FaultException("Erreur GetCustomer : "+ ex.Message);
            }
        }

        public List<CustomerDC> GetAllCustomer()
        {
            try
            {
                return CustomerDAC.GetAllCustomer();

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetCustomer : " + ex.Message);
            }
        }

        public Boolean AddCustomerFromUser(CustomerDC dc)
        {
            try
            {
                return CustomerDAC.AddCustomer(dc);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur AddCustomerFromUser : " + ex.Message);
            }
        }

        public Boolean AddCustomerFromForum(string email, AgencyEmployeeDC employee)
        {
            try
            {
                return CustomerDAC.AddCustomer(email, employee);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur AddCustomerFromForum : " + ex.Message);
            }
        }

        public Boolean AddCodeToUser(CodeDC code)
        {
            try
            {
                return CodeDAC.AddCode(code );

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur AddCodeToUser : " + ex.Message);
            }
        }

        public Boolean AddSessionToUser(int sessionID, int customerID)
        {
            try
            {
                return SessionCoursesDAC.AddCustomerToSession(sessionID, customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur AddSessionToUser : " + ex.Message);
            }
        }

       

        public List<SessionDC> GetSessionsByCustomerID(int customerID)
        {
            try
            {
                return SessionCoursesDAC.GetSessionsByCustomer(customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetSessionsByCustomerID : " + ex.Message);
            }
        }

        public List<SessionByCustomer_Result> GetAllSessionsByCustomerAndOffer(int idCustomer, int idOffer, DateTime date)
        {
            try
            {
                return SessionCoursesDAC.GetAllSessionAgencyBycustomerAndOffer(idCustomer, idOffer, date);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetAllSessionsView : " + ex.Message);
            }
        }

        public List<CodeDC> GetCodesByCustomerID(int customerID)
        {
            try
            {
                return CodeDAC.GetCodeByUser(customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetCodesByCustomerID : " + ex.Message);
            }
        }

        public int DeleteCodeToCustomer(int idCode)
        {
            try
            {
               return  CodeDAC.DeletCode(idCode);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur DeleteCodeToCustomer : " + ex.Message);
            }
        }

        public int DeleteSessionToCustomer(int id, int idcustomer)
        {
            try
            {
               return SessionCoursesDAC.DeleteCustomerToSession(id, idcustomer);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur DeleteSessionToCustomer : " + ex.Message);
            }
        }

        public List<FactureDC> GetBillsByCustomerID(int customerID)
        {
            try
            {
                return FactureDAC.GetCustomerFacture(customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetBillsByCustomerID : " + ex.Message);
            }
        }

        public Boolean AddExamToUser(int idExam, int customerID)
        {
            try
            {
                return ExamDAC.AddExamToCustomer(idExam, customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetBillsByCustomerID : " + ex.Message);
            }
        }

        public void PayUserBill(int customerID, List<string> detailList, List<SessionToPayDC> sessionsToPay)
        {
            try
            {
                FactureDAC.PayBill(customerID, detailList, sessionsToPay );
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur payUserBill : " + ex.Message);
            }
        }

        public List<OfficialTestDC> GetExamsByCustomerID(int customerID)
        {
            try
            {
                return ExamDAC.GetCustomerExams(customerID);
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetExamsByCustomerID : " + ex.Message);
            }
        }

        public int GetAmountToPayByCustomerID(int customerID)
        {
            try
            {
                return FactureDAC.GetAmountToPayByCustomerID(customerID);
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetExamsByCustomerID : " + ex.Message);
            }
        }

        public List<FactureDC> GetAllFactures()
        {
            try
            {
                return FactureDAC.GetAllFactures();
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetExamsByCustomerID : " + ex.Message);
            }
        }

        public RecapFacturesDC GetRecapFacturesCustomer(int customerID)
        {
            try
            {
                return RecapFacturesDAC.GetRecapFacturesByCustomer(customerID);
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetExamsByCustomerID : " + ex.Message);
            }
        }

        public int DeleteExamToCustomer(int idExam, int Idcustomer)
        {
            try
            {
               return ExamDAC.DeleteExam(idExam, Idcustomer);
            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur DeleteExamToCustomer : " + ex.Message);
            }
        }

        #region Cross Domain

        public Stream GetSilverlightPolicy()
        {
            return StringToStream(CommonConstant.clientaccesspolicy);
        }
        public Stream GetFlashPolicy()
        {
            return StringToStream(CommonConstant.crossdomain);
        }

        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }

        #endregion



        public CustomerInformationDC GetCustomerInformation(int customerID)
        {
            try
            {
                return CustomerDAC.getCustomerInformation(customerID);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetCustomerInformation : " + ex.Message);
            }
        }


        public int DeleteCustomer(int id)
        {
            try
            {
                return CustomerDAC.Deletecustomer(id);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetCustomerInformation : " + ex.Message);
            }
        }


        public List<ExamsByCustomer_Result> GetAllExamsAgencyByCustomer(int customerId)
        {
            try
            {
                return ExamDAC.GetAllExamsAgencyByCustomer(customerId);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetBillsByCustomerID : " + ex.Message);
            }
        }

        public AgencyEmployeeDC checkLogin(string login, string password)
        {
            try
            {
                return AgencyEmployeeDAC.checkLogin(login, password);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetBillsByCustomerID : " + ex.Message);
            }
        }


        public byte[] getFatcure(int id)
        {
            try
            {
                return FactureDAC.getFactures(id);

            }
            catch (Exception ex)
            {
                throw new FaultException("Erreur GetBillsByCustomerID : " + ex.Message);
            }
        }
    }
}
